package com.ksm.client.document.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.ksm.client.ManagerEventBus;
import com.ksm.client.document.JustificatifProperties;
import com.ksm.client.document.presenter.DocumentJustificatifPresenter.IDocumentJustificatifView;
import com.ksm.client.document.service.IKsmServiceAsync;
import com.ksm.client.document.view.DocumentJustificatifView;
import com.ksm.transfer.dto.DocumentDTO;
import com.ksm.transfer.dto.JustificatifDTO;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.mvp4g.client.view.ReverseViewInterface;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.info.Info;

@Presenter(view = DocumentJustificatifView.class)
public class DocumentJustificatifPresenter extends BasePresenter<IDocumentJustificatifView, ManagerEventBus> implements IDocumentJustificatifPresenter {

	@Inject
	IKsmServiceAsync ksmService;

	private final JustificatifProperties justificatifProperties = GWT.create(JustificatifProperties.class);

	public interface IDocumentJustificatifView extends IsWidget, ReverseViewInterface<IDocumentJustificatifPresenter> {

		public void addToStore(JustificatifDTO jDto);

		public void setListStore(ListStore<JustificatifDTO> listeStore, Long documentId);

		public Long getSelectionnedDocumentId();

		public void setSelectionnedDocumentId(Long selectionnedDocumentId);

		public String getUploadedFileName();

		public void setUploadedFileName(String uploadedFileName);
	}

	@Override
	public void saveDocument() {

		JustificatifDTO justificatifDTO = new JustificatifDTO();
		justificatifDTO.setDocumentId(getView().getSelectionnedDocumentId());
		justificatifDTO.setName(getView().getUploadedFileName());
		justificatifDTO.setLocation(getView().getUploadedFileName());

		ksmService.saveJustificatif(justificatifDTO, new AsyncCallback<JustificatifDTO>() {

			@Override
			public void onSuccess(JustificatifDTO justificatifDTO) {
				Info.display("Save OK", "Saving OK ");
				getView().addToStore(justificatifDTO);
				showPreviewJustifcatif(justificatifDTO);

			}

			@Override
			public void onFailure(Throwable throwable) {
				Info.display("Save Error", "Saving error " + throwable.getMessage());

			}
		});
	}

	public void onDocumentSaved() {

	}

	@Override
	public void onDocumentNewLoad() {
		getView().setListStore(new ListStore<JustificatifDTO>(justificatifProperties.key()), 0L);
	}

	@Override
	public void onDocumentGridLoad(DocumentDTO documentDTO) {

		ListStore<JustificatifDTO> justficatifStores = new ListStore<JustificatifDTO>(justificatifProperties.key());

		justficatifStores.addAll(documentDTO.getJustificatifs());

		getView().setListStore(justficatifStores, documentDTO.getId());

	}

	@Override
	public void showPreviewJustifcatif(JustificatifDTO justificatifDTO) {
		getEventBus().justificatifGridLoad(justificatifDTO);
	}
}
